package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.FourGEmergencyBroadcastTrumpAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <Description> <br>
 *
 * @author LiuShaojie<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021-12-20-10:50 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler <br>
 */
@Service
@AttrHandler(FourGEmergencyBroadcastTrumpAttrHandler.class)
public class FourGEmergencyBroadcastTrumpAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String attrValue = attr.getValue();
        String custId = data.getCustId();
        String prodInstId = inst.getInstId();
        // 结算价属性处理
        KiteAttr settleAttr = querySceneInstService.getInstAttr(custId, InstType.PROD_INST, prodInstId, "200004108");
        //销售价属性处理
        KiteAttr sellAttr = querySceneInstService.getInstAttr(custId,InstType.PROD_INST, prodInstId,"200004107");
        String sceneInstId = data.getSceneInstId();//获取场景ID
        //型号对应的销售价
        if (Objects.nonNull(sellAttr)) {
            //乡村号角-3199元
            setHandleAttrValue ("110007195", attrValue, inst, "200004107", "3199", sceneInstId);
            //key:型号枚举值ID  newFieldName:结算价的属性编码   newValue:结算价的枚举值ID
            //城镇音柱-2899元
            setHandleAttrValue ("110007196", attrValue, inst, "200004107", "2899", sceneInstId);
        }
        if(Objects.nonNull(settleAttr)){
            //乡村号角-2699元
            setHandleAttrValue ("110007195", attrValue, inst, "200004108", "2699", sceneInstId);
            //key:型号枚举值ID  newFieldName:结算价的属性编码   newValue:结算价的枚举值ID
            //城镇音柱-2499元
            setHandleAttrValue ("110007196", attrValue, inst, "200004108", "2499", sceneInstId);
        }
        // 联动同场景下其他产品的型号
        setOthProdInstMode(attrValue, inst, sceneInstId);
    }

    private void setOthProdInstMode(String value, KiteInst inst, String sceneInstId) {
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(sceneInstId);
        kiteProdInstList.stream().forEach(prodInst ->{
            // 找到型号
            KiteAttr kiteAttr = prodInst.getAttr("200002772");

            if (Objects.nonNull(kiteAttr) && !KiteStringUtils.isEqual(inst.getInstId(), prodInst.getProdInstId())) {
                List<KiteAttrValue> kiteAttrValue = kiteAttr.getAttrValues();
                //value是主控板型号枚举值id
                if (KiteStringUtils.equalsAny(value, "110007195")) {
                    List<KiteAttrValue> attrValues = kiteAttrValue.stream().filter(AttrValue -> KiteStringUtils.equalsAny(AttrValue.getAttrValue(), "110007195")).collect(Collectors.toList());
                    if (KiteListUtils.isNotEmpty(attrValues)) {
                        //主框架型号和应急广播大喇叭电子元件型号 联动  乡村号角
                        operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), attrValues.get(0).getAttrValue(), sceneInstId, null);

                        //主框架型号与产品公允价联动
                        //抗汛防洪大喇叭产品(乡村号角)-2569元
                        if (KiteStringUtils.isEqual("110007195", value) && KiteStringUtils.isEqual("110007195", attrValues.get(0).getAttrValue()) && KiteStringUtils.isEqual("63249222",prodInst.getSpecId())) {
                            operateInstAttrService.setAttrValue(prodInst, "200004115", "2569", sceneInstId, null);
                        }
                        //主控板型号与物资编码联动
                        //抗汛防洪大喇叭产品-乡村号角-物资编码-7600300920
                        if(KiteStringUtils.isEqual("110007195", attrValues.get(0).getAttrValue()) && KiteStringUtils.isEqual("63249222",prodInst.getSpecId())){
                            operateInstAttrService.setAttrValue(prodInst, "200004178", "7600300921", sceneInstId, null);
                        }
                    }
                }
                //value是附加型号枚举值id
                if (KiteStringUtils.equalsAny(value, "110007196")) {
                    List<KiteAttrValue> attrValues = kiteAttrValue.stream().filter(AttrValue -> KiteStringUtils.equalsAny(AttrValue.getAttrValue(),"110007196")).collect(Collectors.toList());
                    if (KiteListUtils.isNotEmpty(attrValues)) {
                        //主框架型号和应急广播大喇叭电子元件型号 联动
                        operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), attrValues.get(0).getAttrValue(), sceneInstId, null);

                        //主框架型号与产品公允价联动
                        //抗汛防洪大喇叭产品(城镇音柱)-2169元
                        if (KiteStringUtils.isEqual("110007196", value) && KiteStringUtils.isEqual("110007196", attrValues.get(0).getAttrValue()) && KiteStringUtils.isEqual("63249222",prodInst.getSpecId())) {
                            operateInstAttrService.setAttrValue(prodInst, "200004115", "2169", sceneInstId, null);
                        }
                        //主控板型号与物资编码联动
                        //抗汛防洪大喇叭产品-城镇音柱-物资编码-7600300920
                        if(KiteStringUtils.isEqual("110007196", attrValues.get(0).getAttrValue()) && KiteStringUtils.isEqual("63249222",prodInst.getSpecId())){
                            operateInstAttrService.setAttrValue(prodInst, "200004178", "7600300920", sceneInstId, null);
                        }
                    }
                }
            }
        });
    }
    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {
        // TODO Auto-generated method stub
    }

    private void setHandleAttrValue (String key, String oldValue, KiteInst inst, String newFieldName,
                                     String newValue, String sceneInstId ) {
        if (KiteStringUtils.isEqual(key, oldValue)) {
            operateInstAttrService.setAttrValue(inst, newFieldName, newValue, sceneInstId, null);
        }

    }
}
